package cn.itcast;

import java.util.ArrayList;
import java.util.List;

public class ArrayList2 {
    public static void main(String[] args) {
        List<Integer> list2 = List.of(11, 22, 33);
        List<Integer> list = new ArrayList<>();
        //添加
        list.add(1000); //0
        list.add(500);//1
        list.add(0, 100);//0 100 1 1000 2 500
        list.add(1, 50);
        list.addAll(list2);
        list.addAll(0, List.of(111, 222, 333));
        list.addAll(0, List.of(111, 222, 333));
        list.addAll(0, List.of(111, 222, 333));
        System.out.println(list);
        System.out.println(list.size());

        //判断
        System.out.println(list.contains(111));//true
        System.out.println(list.indexOf(222));//1
        System.out.println(list.indexOf(2223));//-1
        System.out.println(list.containsAll(List.of(111, 222, 444)));//false
        System.out.println(list.lastIndexOf(222));//7

        //修改
        list.set(0, 1100);
        list.set(2, 2);
        System.out.println(list);
        //list.replaceAll(e -> e + 10);
        list.replaceAll(e -> e == 222 ? 555 : e);
        System.out.println(list);

        //删除
        //list.clear();
        int id = 2;
        list.remove(id); //删除索引位置
        Integer ids = 222;
        list.remove(ids);//删除对象
        list.removeAll(List.of(222));//删除所有对象222
        list.removeIf(e -> e == 111 || e == 333);//删除所有111 所有333
        System.out.println(list);
        //list.removeIf(a->true);//删除所有
        list.removeIf(a -> Math.random() > .5);//随机删除元素
        System.out.println(list);


        //遍历
        for(int i : list){
            System.out.println(i);
        }
        for (Integer integer : list) {
            System.out.println(integer);
        }
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }
        for (int i = list.size() - 1; i >= 0; i--) {
            System.out.println(list.get(i));
        }

        System.out.println("--------------------");
        //lambda表达式，通过forEach() 方法
        //list.forEach(System.out::println);
        list.forEach(e->{
            if(e == 555){
                return;  //forEach()是方法，不是循环语句，不可以使用break continue,但可以使用return;
            }
            System.out.println(e);
        });

    }

}
